package com.liwang.lanouhn.nvshengo;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.liwang.lanouhn.nvshengo.fragments.ClothespressFragment;
import com.liwang.lanouhn.nvshengo.fragments.CosmeticFragment;
import com.liwang.lanouhn.nvshengo.fragments.LivesFragment;
import com.liwang.lanouhn.nvshengo.fragments.SelfFragment;



public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RadioButton rb_sort;
    private TextView tv_title;
    private ImageView iv_search;
    private RadioButton rb_cosmetic;
    private RadioButton rb_clothespress;
    private RadioButton rb_lives;
    private RadioButton rb_body;
    private FrameLayout framelayout;
    private FragmentManager fm =getSupportFragmentManager();
    private CosmeticFragment cosmetic;
    private ClothespressFragment clothespress;
    private LivesFragment lives;
    private SelfFragment self;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        Window window=getWindow();
        window.setFlags(flag,flag);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }
    long waitTime = 2000;
    long touchTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN && KeyEvent.KEYCODE_BACK == keyCode) {
            long currentTime = System.currentTimeMillis();
            if((currentTime-touchTime)>=waitTime) {
                //让Toast的显示时间和等待时间相同
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                touchTime = currentTime;
            }else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



    private void initView() {
        rb_sort = (RadioButton) findViewById(R.id.rb_sort);
        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_search = (ImageView) findViewById(R.id.iv_search);
        rb_cosmetic = (RadioButton) findViewById(R.id.rb_cosmetic);
        rb_clothespress = (RadioButton) findViewById(R.id.rb_clothespress);
        rb_lives = (RadioButton) findViewById(R.id.rb_lives);
        rb_body = (RadioButton) findViewById(R.id.rb_body);
        framelayout= (FrameLayout) findViewById(R.id.framelayout);
        rb_sort.setOnClickListener(this);
        iv_search.setOnClickListener(this);
        rb_cosmetic.setOnClickListener(this);
        rb_clothespress.setOnClickListener(this);
        rb_lives.setOnClickListener(this);
        rb_body.setOnClickListener(this);
    }
    private void initData() {
        cosmetic=new CosmeticFragment();
        clothespress=new ClothespressFragment();
        lives=new LivesFragment();
        self=new SelfFragment();
        FragmentTransaction transaction =fm.beginTransaction();
        transaction.add(R.id.framelayout,cosmetic);
        transaction.add(R.id.framelayout,clothespress);
        transaction.add(R.id.framelayout,lives);
        transaction.add(R.id.framelayout,self);
        transaction.show(cosmetic);
        transaction.hide(clothespress);
        transaction.hide(lives);
        transaction.hide(self);
        transaction.commit();

    }


    @Override
    public void onClick(View view) {
        FragmentTransaction ft = fm.beginTransaction();
        switch (view.getId()){
            case R.id.rb_cosmetic:
                tv_title.setText("美丽妆容");
                rb_sort.setVisibility(View.GONE);
                iv_search.setVisibility(View.VISIBLE);
                ft.show(cosmetic);
                ft.hide(clothespress);
                ft.hide(lives);
                ft.hide(self);
                break;
            case R.id.rb_clothespress:
                tv_title.setText("美丽购");
                rb_sort.setVisibility(View.VISIBLE);
                iv_search.setVisibility(View.VISIBLE);
                ft.hide(cosmetic);
                ft.show(clothespress);
                ft.hide(lives);
                ft.hide(self);
                break;
            case R.id.rb_lives:
                tv_title.setText("女神Show");
                rb_sort.setVisibility(View.GONE);
                iv_search.setVisibility(View.VISIBLE);
                ft.hide(cosmetic);
                ft.hide(clothespress);
                ft.show(lives);
                ft.hide(self);
                break;
            case R.id.rb_body:
                tv_title.setText("美丽小屋");
                rb_sort.setVisibility(View.GONE);
                iv_search.setVisibility(View.VISIBLE);
                ft.hide(cosmetic);
                ft.hide(clothespress);
                ft.hide(lives);
                ft.show(self);
                break;
        }
        ft.commit();
    }
}
